package com.example.common.widget.state;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import com.example.common.R;

import java.util.HashMap;
import java.util.Map;

public class NetworkStateLayout extends FrameLayout implements NetworkStateView{

    public static final Integer ERROR_NETWORK_STATE = 1;
    public static final Integer NOT_NETWORK_STATE = 2;
    public static final Integer LOADING_NETWORK_STATE = 3;
    public static final Integer EMPTY_NETWORK_STATE = 4;

    @SuppressLint("UseSparseArrays") private Map<Integer, NetworkState> mStateMap = new HashMap<>();

    public NetworkStateLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        // 无网络
        NetworkState notNetworkStateView = new NotNetworkStateView();
        mStateMap.put(notNetworkStateView.getStateCode(), notNetworkStateView);

        // 空状态
        EmptyNetworkStateLayout emptyNetworkStateLayout = new EmptyNetworkStateLayout();
        mStateMap.put(emptyNetworkStateLayout.getStateCode(), emptyNetworkStateLayout);

        // 网络加载中
        NetworkState loadingNetworkStateView = new LoadingNetworkStateView();
        mStateMap.put(loadingNetworkStateView.getStateCode(), loadingNetworkStateView);

        // 错误
        ErrorNetworkStateView errorNetworkStateView = new ErrorNetworkStateView();
        mStateMap.put(errorNetworkStateView.getStateCode(), errorNetworkStateView);
    }

    public NetworkStateLayout addNetworkStateView(NetworkState networkState) {
        mStateMap.put(networkState.getStateCode(), networkState);
        return this;
    }

    public void showContentView() {
        for (int i = 0; i < getChildCount(); i++) getChildAt(i).setVisibility(View.GONE);
        getChildAt(0).setVisibility(VISIBLE);
    }

    public void showNetworkStateView(int stateCode) {
        showNetworkStateView(stateCode, null);
    }

    public void showNetworkStateView(int stateCode, CharSequence charSequence) {
        NetworkState networkState = mStateMap.get(stateCode);

        if (networkState == null) {
            throw new IllegalStateException(getResources().getString(R.string.there_is_no_state));
        }

        if (networkState.getNetworkStateView() == null) {
            networkState.bindParentView(this);
        }

        if (!TextUtils.isEmpty(charSequence)) {
            networkState.showView(charSequence);
        }
    }

    public void setOnAnewRequestNetworkListener(OnAnewRequestNetworkListener onAnewRequestNetworkListener) {
        for (Map.Entry<Integer, NetworkState> networkStateEntry : mStateMap.entrySet()) {
            networkStateEntry.getValue().setOnAnewRequestNetworkListener(onAnewRequestNetworkListener);
        }
    }

    @Override
    public void notNetwork() {
        showNetworkStateView(NOT_NETWORK_STATE);
    }

    @Override
    public void loadingNetwork() {
        showNetworkStateView(LOADING_NETWORK_STATE);
    }

    @Override
    public void successNetwork() {
        showContentView();
    }

    @Override
    public void emptyNetwork() {
        showNetworkStateView(EMPTY_NETWORK_STATE);
    }

    @Override
    public void errorNetwork(CharSequence charSequence) {
        showNetworkStateView(ERROR_NETWORK_STATE, charSequence);
    }

    @Override
    public void onAnewRequestNetwork() {

    }

    protected interface NetworkState {

        Integer getStateCode();

        View getNetworkStateView();

        void bindParentView(NetworkStateLayout parentView);

        void showView(CharSequence message);

        void setOnAnewRequestNetworkListener(OnAnewRequestNetworkListener onAnewRequestNetworkListener);
    }
}
